package cn.bdqn.service.questionBank;

import cn.bdqn.dao.questionBank.SubjectDao;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.dto.questionBank.subject.SubjectDTO;
import cn.bdqn.dto.questionBank.subject.SubjectPageDTO;
import cn.bdqn.pojo.questionBank.OperationRecord;
import cn.bdqn.pojo.questionBank.Subject;
import cn.bdqn.utils.Json;
import cn.bdqn.vo.questionBank.subject.SubjectVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("subjectService")
public class SubjectService {

    @Autowired
    private SubjectDao subjectDao;

    @Autowired
    private OperationRecordService operationRecordService;

    /**
     * 查询题目
     * @param subjectTypeId
     * @param accountTypeId
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Json selectSubject(Integer subjectTypeId,Integer accountTypeId,Integer status,Integer pageNumber,Integer pageSize){
        if(subjectTypeId == 0){
            subjectTypeId = null;
        }
        if(accountTypeId == 0){
            accountTypeId = null;
        }
        //System.out.println(status);
        if(status == 0){
            status = null;
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<SubjectVo> subjectVoList = subjectDao.selectSubject(subjectTypeId,accountTypeId,status);
        PageInfo<SubjectVo> page = new PageInfo<>(subjectVoList);
        Json json = new Json(200,"查询成功",page);
        return json;
    }
    /**
     * 查询题目
     * @param subjectPageDTO
     * @return
     */
    public Json selectSubject(SubjectPageDTO subjectPageDTO){
        if(subjectPageDTO.getSubjectTypeId() == 0){
            subjectPageDTO.setSubjectTypeId(null);
        }
        if(subjectPageDTO.getAccountTypeId() == 0){
            subjectPageDTO.setAccountTypeId(null);
        }
        System.out.println(subjectPageDTO.getStatus());
        if(subjectPageDTO.getStatus() == 0){
            subjectPageDTO.setStatus(null);
        }
        PageHelper.startPage(subjectPageDTO.getPageNumber(), subjectPageDTO.getPageSize());
        List<SubjectVo> subjectVoList = subjectDao.selectSubject(subjectPageDTO.getSubjectTypeId(), subjectPageDTO.getAccountTypeId(), subjectPageDTO.getStatus());
        PageInfo<SubjectVo> page = new PageInfo<>(subjectVoList);
        Json json = new Json(200,"查询成功",page);
        return json;
    }

    /**
     * 添加多个题目
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertSubjectList(List<SubjectDTO> list, Integer accountTypeId, Integer userId, SubjectPageDTO subjectPageDTO) throws Exception {
        OperationRecord operationRecord =  operationRecordService.operationRecordPublicMethod(userId,"批量添加题目",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录异常");
        }

        int i = subjectDao.insertSubjectList(list,accountTypeId,operationRecord.getOperationRecordId());
        if(i != list.size()){
            throw new Exception("题目添加失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 添加一个题目信息
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json insertOneSubject(SubjectDTO subjectDTO,Integer accountTypeId, Integer userId, SubjectPageDTO subjectPageDTO) throws Exception {
        OperationRecord operationRecord =  operationRecordService.operationRecordPublicMethod(userId,"批量添加题目",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录异常");
        }
        Subject subject = new Subject();
        subject.setSubjectTitle(subjectDTO.getSubjectTitle());
        subject.setSubjectTypeId(subjectDTO.getSubjectType());
        subject.setStatus(2);
        subject.setAccountTypeId(accountTypeId);
        subject.setCreateOperationRecordId(operationRecord.getOperationRecordId());
        subject.setSubjectOptionAnswer(subjectDTO.getSubjectXuanDaan());
        subject.setSubjectRightKey(subjectDTO.getSubjectRight());
        subject.setIsDel(1);
        subject.setUpdateOperationRecordId(0);
        int i = subjectDao.insert(subject);
        if(i != 1){
            throw new Exception("题目添加异常");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 删除一条题目信息
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json deleteOneSubject(Integer subjectId,Integer userId,SubjectPageDTO subjectPageDTO) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"添加一条题目",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw  new Exception("操作记录表添加失败");
        }

        int a = subjectDao.update(new Subject(),new UpdateWrapper<Subject>().set("is_del",0).eq("subject_id",subjectId));
        if(a != 1){
            throw new Exception("题目删除失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 修改题目信息
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json updateOneSubject(Subject subject,String operationRecordRemarks,Integer accountTypeId, Integer userId, SubjectPageDTO subjectPageDTO) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,operationRecordRemarks,new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw  new Exception("操作记录添加失败");
        }
        subject.setUpdateOperationRecordId(operationRecord.getOperationRecordId());
        int a = subjectDao.update(subject,new UpdateWrapper<Subject>().eq("subject_id",subject.getSubjectId()));

        System.out.println(subject);
        System.out.println(a);
        if(a != 1){
            throw new Exception("更新失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 发布一个
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json statusOneSubject(Integer userId,Integer subjectId,SubjectPageDTO subjectPageDTO) throws Exception {
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"发布题目id为"+subjectId,new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录失败");
        }

        int a = subjectDao.update(null,new UpdateWrapper<Subject>().set("status",1).eq("subject_id",subjectId));
        if(a != 1){
            throw new Exception("发布失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 批量删除
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json deleteListSubject(Integer userId,List<Integer> list,SubjectPageDTO subjectPageDTO) throws Exception {
        if(list.size() == 0){
            throw new Exception("无数据操作");
        }
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"批量删除题目",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录失败");
        }

        int a = subjectDao.deleteListSubject(list);
        if(a != list.size()){
            throw new Exception("批量删除失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 批量发布
     * @param userId
     * @param list
     * @param subjectPageDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Json statusListSubject(Integer userId,List<Integer> list,SubjectPageDTO subjectPageDTO) throws Exception {
        if(list.size() == 0){
            throw new Exception("无数据操作");
        }
        OperationRecord operationRecord = operationRecordService.operationRecordPublicMethod(userId,"批量发布题目",new SubjectAccountTypeDTO());
        if(operationRecord == null){
            throw new Exception("操作记录失败");
        }

        int a = subjectDao.statusListSubject(list);
        if(a != list.size()){
            throw new Exception("发布失败");
        }
        return selectSubject(subjectPageDTO);
    }

    /**
     * 随机获取题目
     * @param size 数量
     * @param accountTypeId 科目类型
     * @param subjectTypeId 题目类型
     * @return
     */
    public List<Integer> getSubjectIdList(Integer accountTypeId, Integer subjectTypeId,Integer size){
        return subjectDao.getSubjectIdList(accountTypeId,subjectTypeId,size);
    }

    /**
     * 用户选择的题目
     * @param subjectPageDTO
     * @param subjectId
     * @return
     */
    public Json userSelectSubject(SubjectPageDTO subjectPageDTO,List<Integer> subjectId){
        if(subjectId.size() == 0){
            subjectId = null;
        }
        PageHelper.startPage(subjectPageDTO.getPageNumber(),subjectPageDTO.getPageSize());
        List<Subject> subjectVoList = subjectDao.userSelectSubjectList(subjectPageDTO.getSubjectTypeId(),subjectPageDTO.getAccountTypeId(),subjectId);
        PageInfo<Subject> page = new PageInfo<>(subjectVoList);
        Json json = new Json(200,"查询成功",page);
        return json;
    }


}
